package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.base.utils.weixin.utils.PayCommonUtil;
import com.qy.base.utils.weixin.utils.WXAPPConfigUtil;
import com.qy.model.*;
import com.qy.service.*;
import com.github.pagehelper.PageHelper;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/policy")
public class PolicyController {
    @Resource
    private PolicyService policyService;

    @Resource
    private PolicyOrderService policyOrderService;

    @Resource
    private EnshrineService enshrineService;

    @Resource
    private MemberService memberService;

    @Resource
    private ApplyService applyService;

    @Resource
    private TollService tollService;

    @PostMapping("/add")
    public Result add(@RequestBody Policy policy) {
        policyService.save(policy);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        policyService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Policy policy) {
        policyService.update(policy);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Policy policy = policyService.findById(id);
        return ResultGenerator.successResult(policy);
    }

    @GetMapping("/list")
    public Result list(PageBean<Policy> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Policy> list = policyService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //热门政策
    @GetMapping("/hotPolicy")
    public Result hotPolicy(PageBean<Policy> page, Policy policy) {
        PageHelper.startPage(page.getPageNum(), 3, "add_time desc, brower_number desc");
        List<Policy> list = policyService.selectAllPolicy(policy);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //政策列表
    @GetMapping("/policyList")
    public Result policyList(PageBean<Policy> page, Policy policy) {
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Policy> list = policyService.selectAllPolicy(policy);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //政策详情
    @GetMapping("/policyDetail")
    public Result policyDetail(Policy policy) {
        if (policy.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Policy> list = policyService.selectPolicyById(policy);
        Integer visibleState = 1;//1不可见 2可见
        Integer enshrineState = 1;//1未收藏 2已收藏
        Integer applyState = 1;//1未申请 2已申请
        Integer registerState = 1;//1未注册 2已注册
        Map<String, Object> map = new HashMap<>();
        map.put("visibleState", visibleState);
        map.put("enshrineState", enshrineState);
        map.put("applyState", applyState);
        map.put("registerState", registerState);
        map.put("policy_surplus_number", 0);//政策剩余条数
        map.put("member_surplus_number", 0);//会员剩余条数

        Toll toll = tollService.selectOnePolicyPrice();
        map.put("onePolicyPrice", toll.getAmount());//单条政策价格

        if (policy.getUser_id() != null) {
            //判断是否可见
            visibleState = policyOrderService.selectVisibleState(policy.getUser_id(), policy.getId(), 1);
            //判断是否收藏
            enshrineState = enshrineService.selectEnshrineState(policy.getUser_id(), policy.getId(), 1);
            //判断是否申请
            List<Apply> applyList = applyService.selectApplyListById(policy);
            if (applyList.size() == 0) {
                applyState = 1;
            } else {
                applyState = 2;
            }

            Member member = memberService.findById(policy.getUser_id());

            //判断是否注册
            if (member.getPhone() == null || member.getPhone().isEmpty()) {
                registerState = 1;
            } else {
                registerState = 2;
            }

            map.put("visibleState", visibleState);
            map.put("enshrineState", enshrineState);
            map.put("applyState", applyState);
            map.put("registerState", registerState);
            map.put("policy_surplus_number", member.getPolicy_surplus_number());
            map.put("member_surplus_number", member.getMember_surplus_number());
        }
        return ResultGenerator.successResult(list, map);
    }

}
